package com.bp.hearthealth.pulse.lnapps.bp_utils

import android.content.Context
import android.content.SharedPreferences
import android.text.TextUtils
import android.util.Base64
import com.bp.hearthealth.pulse.lnapps.BpApplication
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import java.io.Serializable
import java.text.SimpleDateFormat
import java.util.Date


object SpUtils {
    private const val name = "sp_config.cfg"
    private var sp: SharedPreferences? = null
    fun saveInt(key: String?, value: Int) {
        if (sp == null) {
            sp = BpApplication.context.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        sp!!.edit().putInt(key, value).commit()
    }

    fun saveInt(ctx: Context, key: String?, value: Int) {
        if (sp == null) {
            sp = ctx.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        sp!!.edit().putInt(key, value).commit()
    }

    fun getInt(key: String?): Int {
        if (sp == null) {
            sp = BpApplication.context.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        return sp!!.getInt(key, -1)
    }

    fun getInt(ctx: Context, key: String?): Int {
        if (sp == null) {
            sp = ctx.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        return sp!!.getInt(key, -1)
    }

    fun getInt(ctx: Context, key: String?, defValue: Int): Int {
        if (sp == null) {
            sp = ctx.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        return sp!!.getInt(key, defValue)
    }

    fun saveBoolean(key: String?, value: Boolean) {
        if (sp == null) {
            sp = BpApplication.context.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        sp!!.edit().putBoolean(key, value).commit()
    }

    fun saveBoolean(ctx: Context, key: String?, value: Boolean) {
        if (sp == null) {
            sp = ctx.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        sp!!.edit().putBoolean(key, value).commit()
    }

    fun getBoolean(key: String?): Boolean {
        if (sp == null) {
            sp = BpApplication.context.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        return sp!!.getBoolean(key, false)
    }

    fun getBoolean(key: String?, isTrue: Boolean): Boolean {
        if (sp == null) {
            sp = BpApplication.context.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        return sp!!.getBoolean(key, isTrue)
    }

    fun getBoolean(ctx: Context, key: String?): Boolean {
        if (sp == null) {
            sp = ctx.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        return sp!!.getBoolean(key, false)
    }

    fun getBoolean(ctx: Context, key: String?, defValue: Boolean): Boolean {
        if (sp == null) {
            sp = ctx.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        return sp!!.getBoolean(key, defValue)
    }

    fun saveLong(ctx: Context, key: String?, value: Long) {
        if (sp == null) {
            sp = ctx.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        sp!!.edit().putLong(key, value).commit()
    }

    fun saveLong(key: String?, value: Long) {
        if (sp == null) {
            sp = BpApplication.context.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        sp!!.edit().putLong(key, value).commit()
    }

    fun getLong(ctx: Context, key: String?, defValue: Long): Long {
        if (sp == null) {
            sp = ctx.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        return sp!!.getLong(key, defValue)
    }

    fun getLong(key: String?, defValue: Long): Long {
        if (sp == null) {
            sp = BpApplication.context.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        return sp!!.getLong(key, defValue)
    }

    fun getLong(key: String?): Long {
        if (sp == null) {
            sp = BpApplication.context.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        return sp!!.getLong(key, -1)
    }

    fun saveFloat(ctx: Context?, key: String?, value: Float) {
        if (sp == null) {
            sp = BpApplication.context.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        sp!!.edit().putFloat(key, value).commit()
    }

    fun getFloat(key: String?): Float {
        if (sp == null) {
            sp = BpApplication.context.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        return sp!!.getFloat(key, -1f)
    }

    fun getFloat(ctx: Context, key: String?): Float {
        if (sp == null) {
            sp = ctx.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        return sp!!.getFloat(key, -1f)
    }

    fun getFloat(ctx: Context, key: String?, defValue: Float): Float {
        if (sp == null) {
            sp = ctx.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        return sp!!.getFloat(key, defValue)
    }

    @Synchronized
    fun saveString(key: String?, value: String?) {
        if (sp == null) {
            sp = BpApplication.context.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        sp!!.edit().putString(key, value).commit()
    }

    @Synchronized
    fun saveString(ctx: Context, key: String?, value: String?) {
        if (sp == null) {
            sp = ctx.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        sp!!.edit().putString(key, value).commit()
    }

    fun getString(key: String): String? {
        if (sp == null) {
            sp = BpApplication.context.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        return sp!!.getString(key, "")
    }

    fun getString(key: String?, defValue: String?): String? {
        if (sp == null) {
            sp = BpApplication.context.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        return sp!!.getString(key, defValue)
    }

    fun getString(ctx: Context, key: String): String? {
        if (sp == null) {
            sp = ctx.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        return sp!!.getString(key, "")
    }

    fun getString(ctx: Context, key: String?, defValue: String?): String? {
        if (sp == null) {
            sp = ctx.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        return sp!!.getString(key, defValue)
    }

    fun remove(ctx: Context, key: String?) {
        if (sp == null) {
            sp = ctx.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        sp!!.edit().remove(key).commit()
    }

    /**
     * 保存序列化过的对象
     *
     * @param key
     * @param obj
     */
    fun saveObj(ctx: Context, key: String?, obj: Any?) {
        if (sp == null) {
            sp = ctx.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        if (obj == null) return
        require(obj is Serializable) { "The object should implements Serializable!" }

        //1.write obj to bytes
        val baos = ByteArrayOutputStream()
        try {
            val oos = ObjectOutputStream(baos)
            oos.writeObject(obj)

            //2.convert obj to string via Base64
            val bytes: ByteArray = Base64.encode(baos.toByteArray(), Base64.DEFAULT)

            //3.save string
            saveString(ctx, key, String(bytes))
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }

    /**
     * 读取序列化过的对象
     *
     * @param key
     * @return
     */
    fun getObj(ctx: Context, key: String?): Any? {
        if (sp == null) {
            sp = ctx.getSharedPreferences(name, Context.MODE_PRIVATE)
        }
        //1.get string
        val string = sp!!.getString(key, null)
        if (TextUtils.isEmpty(string)) return null

        //2.decode string
        val bytes: ByteArray = Base64.decode(string, Base64.DEFAULT)

        //3.read bytes to Object
        val bais = ByteArrayInputStream(bytes)
        try {
            val ois = ObjectInputStream(bais)
            return ois.readObject()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }

    fun isFirstTimeToday(context: Context): Boolean {
        var PREF_NAME = "MyAppPreferences"
        var KEY_LAST_OPEN_DATE = "lastOpenDate"
        val preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
        val lastOpenDate = preferences.getString(KEY_LAST_OPEN_DATE, "")

        // 获取当前日期
        val dateFormat = SimpleDateFormat("yyyy-MM-dd")
        val currentDate = dateFormat.format(Date())

        // 如果上次打开日期为空或者不是今天，则认为是今天第一次打开
        return if (lastOpenDate != currentDate) {
            // 保存当前日期
            val editor = preferences.edit()
            editor.putString(KEY_LAST_OPEN_DATE, currentDate)
            editor.apply()
            true
        } else {
            false
        }
    }

}

